package com.beiding.fastmock.fram;

/**
 * @author 丁常磊
 * @date 2021/7/22 18:33
 */

import javax.servlet.*;
import java.io.IOException;
import java.util.List;

public class VirtualFilterChain implements FilterChain {
    private final FilterChain originalChain;
    private final List<? extends Filter> additionalFilters;
    private int currentPosition = 0;

    public VirtualFilterChain(FilterChain chain, List<? extends Filter> additionalFilters) {
        this.originalChain = chain;
        this.additionalFilters = additionalFilters;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
        if (this.currentPosition == this.additionalFilters.size()) {
            this.originalChain.doFilter(request, response);
        } else {
            ++this.currentPosition;
            Filter nextFilter = (Filter) this.additionalFilters.get(this.currentPosition - 1);
            nextFilter.doFilter(request, response, this);
        }

    }
}
